import { createRouter, createWebHistory } from "vue-router";
import Layout from "@/layout/Layout.vue";
import { useNavtagsStore } from "@/stores";

/**
 * @typedef {Array} routes
 * 路由配置
 * level: 路由层级。
 * sort: 路由排序，用于在路由列表中排序。
 * children: 子路由列表，用于定义子路由。
 * meta: 路由元信息，用于定义路由的标题、图标、是否显示在导航栏中。
 * name: 路由名称，用于在路由中引用。
 * component: 模板，用于定义路由的组件。
 */
const routes = [
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    meta: {
      level: 1,
      sort: 0,
      title: "首页",
      icon: "HomeFilled",
    },
    children: [
      {
        path: "home",
        name: "home",
        component: () => import("@/views/Home/Home.vue"),
        meta: {
          level: 2,
          title: "首页",
          icon: "HomeFilled",
        },
      },
    ],
  },
  {
    path: "/notice",
    component: Layout,
    redirect: "/notice/administration",
    meta: {
      level: 1,
      sort: 1,
      title: "公告",
      icon: "Message",
    },
    children: [
      {
        path: "administration",
        name: "administration",
        level: 2,
        component: () => import("@/views/Notice/Administration.vue"),
        meta: {
          title: "公告管理",
        },
      },
    ],
  },
  {
    path: "/scanning",
    component: Layout,
    redirect: "/scanning/receivingscan",
    meta: {
      level: 1,
      sort: 1,
      title: "扫描",
      icon: "Message",
    },
    children: [
      {
        path: "receivingscan",
        name: "receivingscan",
        level: 2,
        component: () => import("@/views/ScanNing/ReceivingScan.vue"),
        meta: {
          title: "收件扫描",
        },
      },
    ],
  },
  {
    path: "/template",
    component: Layout,
    redirect: "/template/form",
    meta: {
      level: 1,
      sort: 1,
      title: "模板示例",
      icon: "HomeFilled",
    },
    children: [
      {
        path: "form",
        name: "form",
        level: 2,
        component: () => import("@/views/Template/Form.vue"),
        meta: {
          title: "表单组件",
          icon: "HomeFilled",
        },
      },
      {
        path: "table",
        name: "table",
        level: 2,
        component: () => import("@/views/Template/Table.vue"),
        meta: {
          title: "表格组件",
          icon: "HomeFilled",
        },
      },
      {
        path: "drawer",
        name: "drawer",
        level: 2,
        component: () => import("@/views/Template/Drawer.vue"),
        meta: {
          title: "抽屉组件",
          icon: "HomeFilled",
        },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const navtagsStore: any = useNavtagsStore();
  navtagsStore.addTag(to);
  next();
});

export default router;
